﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class AddMatch : System.Web.UI.Page
    {
        MemberAL memberAL = new MemberAL();
        AdminAL adminAL = new AdminAL();
        StudentAL studentAL = new StudentAL();
        SportsAL sportsAL = new SportsAL();
        public Guid SelectedStudent
        {
            get
            {
                if (Session["StudentId"] == null)
                {
                    return studentAL.GetStudents()[0].StudentId;
                }
                else
                {
                    return new Guid(Session["StudentId"].ToString());
                }
            }
            set
            {
                Session["StudentId"] = value;
            }
        }

        public Guid SelectedTeam
        {
            get
            {
                if (Session["TeamId"] == null)
                {
                    return sportsAL.GetAllTeams()[0].TeamId;
                }
                else
                {
                    return new Guid(Session["TeamId"].ToString());
                }
            }
            set
            {
                Session["TeamId"] = value;
            }
        }




        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);
            List<StudentDTO> studentDTO = studentAL.GetStudents();
            List<TeamDTO> teamDTO = sportsAL.GetAllTeams();
            ddlMatchTeam.DataSource = teamDTO;
            ddlMatchTeam.DataTextField = "TeamName";
            ddlMatchTeam.DataValueField = "TeamId";
            ddlMatchTeam.SelectedValue = SelectedTeam.ToString();
            ddlMatchTeam.DataBind();


            ddlMatchBestPerformer.DataSource = studentDTO;
            ddlMatchBestPerformer.DataTextField = "FullName";
            ddlMatchBestPerformer.DataValueField = "StudentId";
            ddlMatchBestPerformer.SelectedValue = SelectedStudent.ToString();
            ddlMatchBestPerformer.DataBind();

            ;

          





        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnMatchSubmit_Click(object sender, EventArgs e)
        {
            SportsAL sportsAL = new SportsAL();
            DateTime matchHeldOn =DateTime.MinValue;
            int matchHeldOnYr = 0;
            Guid teamId = new Guid(ddlMatchTeam.SelectedValue);
            int teamYr = sportsAL.GetTeamById(teamId).SingleOrDefault().Year;

            if (txtMatchHeldOn.Text != "")
            {
                matchHeldOn = Convert.ToDateTime(txtMatchHeldOn.Text);
                matchHeldOnYr = matchHeldOn.Year;

               
            }
            else
            {
                //If they know the date then by default Jan 01 is the match held on date
               
                matchHeldOn = Convert.ToDateTime("01/01/"+ teamYr);
                matchHeldOnYr = Convert.ToInt32(teamYr);
            }


            //Checked played yr & teams yr are differ a lot or not
            if (Math.Abs(matchHeldOnYr - Convert.ToInt32(teamYr)) <= 1)
            {
                try
                {

                    MatchDTO matchDTO = new MatchDTO
                {

                    MatchId = Guid.NewGuid(),
                    MatchName = txtMatchName.Text,
                    TeamId = new Guid(ddlMatchTeam.SelectedValue),
                    OppositeTeamName = txtMatchOppTeam.Text,
                    HeldAt = txtMatchHeldAt.Text,
                    HeldOn = matchHeldOn,
                    Results = txtMatchResult.Text,
                    DetailedResults = txtMatchDetailedResult.Text,
                    TeamBestPerformerId = new Guid(ddlMatchBestPerformer.SelectedValue),
                    // ModifiedBy = "Tharsan",
                    ModifiedTime = System.DateTime.Now
                };

                   string result= sportsAL.saveMatch(matchDTO);
                   if (result == "Saved")
                    {
                        lblAddMatchMsg.Text = "Match Details added Successfully";
                        txtMatchDetailedResult.Text = "";
                        txtMatchHeldAt.Text = "";
                        txtMatchHeldOn.Text = "";
                        txtMatchOppTeam.Text = "";
                        txtMatchResult.Text = "";
                        txtMatchName.Text = "";
                    }
                   else if (result == "Updated")
                   {
                       lblAddMatchMsg.Text = "Match Details updated Successfully";
                       txtMatchDetailedResult.Text = "";
                       txtMatchHeldAt.Text = "";
                       txtMatchHeldOn.Text = "";
                       txtMatchOppTeam.Text = "";
                       txtMatchResult.Text = "";
                       txtMatchName.Text = "";
                   }
                   else if (result == "AlreadyExists")
                    {
                        lblAddMatchMsg.Text = "The Match name added by you is already exists";
                    }
                    else
                    {
                        lblAddMatchMsg.Text = "Your data cannot be added";
                    }

                }
                catch(Exception ee)
                {
                   lblAddMatchMsg.Text="There is an exception occured while inserting the match detail"+e.ToString();
                }
            }
                

            else
            {
                //Give an error message saying that a 2004 team can't play match in < 2002 or > 2006,otherwise team name should be cahnged according to that year
                lblAddMatchMsg.Text = "The year of this team deviates a lot from the year of this match played.";
                txtMatchDetailedResult.Text = "";
                txtMatchHeldAt.Text = "";
                txtMatchHeldOn.Text = "";
                txtMatchOppTeam.Text = "";
                txtMatchResult.Text = "";
                txtMatchName.Text = "";
            }
        }
    }
}
